<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/16
 * Time: 16:25
 */

class tulumen
{
    //数据库连接对象
private static $numbot;
private static $host='127.0.0.1';
private static $user='root';
private static $password='123456';
private static $database='lj';

//初始数据库连接
public static function init($host='',$user='',$password='',$database=''){
if (!empty($host)){
    self::$host=$host;
    //self引用当前类的静态成员变量和静态函数
}
    if (!empty($user)){
        self::$user=$user;
        //self引用当前类的静态成员变量和静态函数
    }
    if (!empty($password)){
        self::$password=$password;
        //self引用当前类的静态成员变量和静态函数
    }
    if (!empty($database)){
        self::$database=$database;
        //self引用当前类的静态成员变量和静态函数
    }


    //创建mysqli对象
    self::$numbot=new mysqli();
    self::$numbot->connect(self::$host,self::$user,self::$password,self::$database);
    if(!empty(self::$numbot->connect_error)){
        echo '数据库连接失败'.self::$numbot->connect_error;
        die;
    }
//    echo '数据库连接成功';
}
//查询
//$sql--查询语句
//返回值：数组
public static function select($sql){
    $data = [];
    $result=self::$numbot->query($sql);
//query规定查询字符串
    if(empty(self::$numbot->error)){
        while (true){
            $row=$result->fetch_assoc();
            if (empty($row)){
                break;
            }
            $data[]=$row;
        }
    }
    //关闭查询结果集
    $result->close();

}
//关闭数据库连接
public static function close(){
    self::$numbot->close();
}
}

